package com.wangsanfei.ihg.wechat.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ibm.icu.text.SimpleDateFormat;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.wangsanfei.util.StringUtils;
import com.wangsanfei.util.UUIDUtils;

public class Notice extends Model<Notice>{

	private static final long serialVersionUID = -8670146965872203947L;
	public static final Notice dao = new Notice();
	
	public Page<Record> pagequery(Integer pageNumber,Integer pageSize,Notice vo,Record record){
		StringBuffer sqlExceptSelect=new StringBuffer("from t_notice t where t.removed='0' ");
		List<Object> list=new ArrayList<Object>();
		
		if(!StringUtils.isEmpty(record.getStr("searchContent"))){
			sqlExceptSelect.append(" and (t.title like ? )");
			list.add("%"+record.getStr("searchContent")+"%");
		}
		
		sqlExceptSelect.append(" order by t.createTime  desc");
		
		if(pageNumber==null){
			pageNumber=1;
		}
		if(pageSize==null){
			pageSize=10;
		}
		return Db.paginate(pageNumber,pageSize,"select t.* ",sqlExceptSelect.toString(),list.toArray());
	}
	
	public Notice add(Notice vo){
		Notice notice=new Notice();
		notice.set("id", UUIDUtils.getUUID());
		notice.set("createTime", new Date());
		notice.set("updateTime", new Date());
		notice.set("updator", vo.getStr("updator"));
		notice.set("creator", vo.getStr("creator"));
		notice.set("removed", "0");
		notice.set("title", vo.getStr("title"));
		notice.set("href", vo.getStr("href"));
		notice.set("href2", vo.getStr("href2"));
		notice.set("status", vo.getStr("status"));
		notice.set("publishTime", vo.getDate("publishTime"));
		notice.set("lx", vo.getStr("lx"));
		if(!StringUtils.isEmpty(vo.getStr("blogo"))){
			notice.set("blogo", vo.getStr("blogo"));
		}
		
		notice.set("content", vo.getStr("content"));
		
		notice.save();
		return notice;
	}
	
	public Notice edit(Notice vo){
		Notice notice=Notice.dao.findById(vo.getStr("id"));
		notice.set("updateTime", new Date());
		notice.set("updator", vo.getStr("updator"));
		notice.set("removed", "0");
		notice.set("title", vo.getStr("title"));
		notice.set("href", vo.getStr("href"));
		notice.set("href2", vo.getStr("href2"));
		notice.set("status", vo.getStr("status"));
		notice.set("publishTime", vo.getDate("publishTime"));
		notice.set("lx", vo.getStr("lx"));
		if(!StringUtils.isEmpty(vo.getStr("blogo"))){
			notice.set("blogo", vo.getStr("blogo"));
		}
		
		notice.set("content", vo.getStr("content"));
		notice.update();
		return notice;
	}
	
	public Notice remove(String id){
		Notice notice=Notice.dao.findById(id);
		notice.set("removed", "1");
		notice.update();
		return notice;
	}
	
	public List<Record> findAll(){
		String sql="select * from t_notice t where t.removed='0' and t.status='0' order by t.createTime desc";
		return Db.find(sql);
	}
	
	public List<Record> findByLx(String lx,Integer limitFrom,Integer pageSize){
		
		SimpleDateFormat sf=new SimpleDateFormat("yyyy年MM月dd日");
		
		String sql="select * from t_notice t where t.removed='0' and t.status='0' and t.lx=? order by t.publishTime desc limit "+limitFrom+","+pageSize+"";
		List<Record> list= Db.find(sql,lx);
		for (Record record : list) {
			if(record.getDate("publishTime")!=null){
				record.set("publishTimeStr", sf.format(record.getDate("publishTime")));
			}
		}
		return list;
	}
}
